class Solution {
    public String longestPalindrome(String s) {
        int n = s.length();
        // dp[i][j] 表示从 i 位置到 j 位置的子串是否是回文串
        // s[i] != s[j]   ->           false
        // s[i] == s[j]   ->   
        // if (i == j || j = i + 1)    true
        // if (i < j)                dp[i + 1][j - 1]
        boolean[][] dp = new boolean[n][n];
        String str = "";
        int maxLen = 0;
        for (int i = n - 1; i >= 0; i--) {
            for (int j = i; j < n; j++) {
                if (s.charAt(i) == s.charAt(j)) {
                    dp[i][j] = i + 1 < j ? dp[i + 1][j - 1] : true;
                } else {
                    dp[i][j] = false;
                }
                if (dp[i][j] && j - i + 1 > maxLen) {
                    maxLen = j - i + 1;
                    str = s.substring(i, j + 1);
                }
            }
        }
        return str;
    }
}